package com.open.capacity.controller;

import com.open.capacity.common.web.Result;
import com.open.capacity.model.UserModel;
import com.open.capacity.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: y_022
 * @date: 19:35-2021/6/23
 * @description:关于用户信息操作的controller！
 */
@Slf4j
@Api(tags = "USER API")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 按用户id查询
     *
     * @param userModel
     * @return
     */
    @GetMapping("/findUserById")
    @Cacheable(value = "Result",key = "#userModel.userid")
    public Result findUserById(UserModel userModel) {
        UserModel userModelByUsername = userService.findUserById(userModel);
        return Result.succeed(userModelByUsername, "操作成功!");
    }

    /**
     * 修改密码
     *
     * @param userModel
     * @return
     */
    @PostMapping("updatePassword")
    public Result updatePassword(UserModel userModel) {
        try {
            userService.updatePassword(userModel);
            return Result.succeed("密码修改完成！");
        } catch (Exception e) {
            log.error("密码修改异常！", e);
        }
        return Result.failed("操作失败!");
    }

    /**
     * 更新用户信息
     *
     * @param userModel
     * @return
     */
    @PostMapping("/updateUser")
    @CacheEvict(value = "Result",key = "#userModel.userid")
    public Result updateUser(UserModel userModel) {
        try {
            userService.updateUser(userModel);
            return Result.succeed("操作成功！");
        } catch (Exception e) {
            log.error("用户信息更新修改异常！", e);
        }
        return Result.failed("操作失败!");
    }
    @PostMapping("/updateAvatar")
    @CacheEvict(value = "Result",key = "#userModel.userid")
    public  Result updateAvatar(UserModel userModel){
        try{
            userService.updateAvatar(userModel);
            return Result.succeed("操作成功！");
        }catch (Exception e){
            log.error("头像更新失败！",e);
        }
        return Result.failed("操作失败！");
    }
}
